﻿using System;
using System.Collections.Generic;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using SmlEngine.UI;
using SmlEngine.UI.Managers;
using SmlEngine.UI.Screens;
using Sml.Scenes;

namespace Sml.Screens
{
    /// <summary>
    /// Encapsulates and manages a title screen, which contains a sample level.
    /// </summary>
    public class TitleScreen : Screen
    {
        private LevelScene background;

        public override IEnumerable<string> PreloadedAssets
        {
            get { return new string[] { }; }
        }

        /// <summary>
        /// Constructs a new TitleScreen instance.
        /// </summary>
        public TitleScreen()
            : base()
        {
            TransitionOnTime = TimeSpan.FromSeconds(2.5);
            TransitionOffTime = TimeSpan.FromSeconds(2.5);
        }

        /// <summary>
        /// Initializes this TitleScreen.
        /// </summary>
        public override void Initialize()
        {
            background = new LevelScene(this);
            Scenes.Add(background);
            background.Initialize();

            base.Initialize();
        }

        /// <summary>
        /// Loads the content for this TitleScreen.
        /// </summary>
        /// <param name="content">The ContentMananer to use.</param>
        public override void LoadContent(ContentManager content)
        {
            background.LoadContent(content);

            using (Stream stream = TitleContainer.OpenStream(@"Content\tests\level1.lvl"))
                background.Load(stream);
        }

        /// <summary>
        /// Handles the keyboard/controller input.
        /// </summary>
        /// <param name="input">The InputManager to use.</param>
        public override void HandleInput(InputManager input)
        {
            //Testing
            if (input.IsNewKeyPress(Microsoft.Xna.Framework.Input.Keys.A))
            {
                ExitScreen();

                MainMenuScreen mainMenuScreen = new MainMenuScreen();
                ScreenGroup s = new ScreenGroup(mainMenuScreen.ScreenManager, mainMenuScreen);
                ScreenManager.AddScreenGroup(s);
            }
        }

        /// <summary>
        /// Draws this TitleScreen.
        /// </summary>
        /// <param name="gameTime">The timing information for the game.</param>
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            ScreenManager.FadeBackBufferToBlack(TransitionAlpha);
        }
    }
}
